package com.like.shopmanager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.like.entity.LoginedUser;
import com.like.likeutils.network.RetrofitCallback;
import com.like.likeutils.network.RetrofitUtil;
import com.like.network.AuthService;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Order;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.activity_login)
public class LoginActivity extends BaseActivity implements Validator.ValidationListener {

    @ViewInject(R.id.username)
    @NotEmpty(message = "用户名不能为空")
    @Order(0)
    private EditText mUsername;
    @ViewInject(R.id.pwd)
    @NotEmpty(message = "密码不能为空")
    @Order(1)
    private EditText mPwd;

    private Validator mValidator;
    private boolean mIsPass = false;
    private AuthService mAuthService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println(JPushInterface.getRegistrationID(m.mContext));
        LoginedUser user = m.getLoginedUser();
        if (user != null) {
            Intent intent = new Intent(m.mContext, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }
        mAuthService = RetrofitUtil.getGsonService(AuthService.class);
        mValidator = new Validator(this);
        mValidator.setValidationListener(this);
    }

    @Event(R.id.login)
    private void loginClick(View v) {
        mValidator.validate();
        if (!mIsPass)
            return;
        m.showLoading(true);
        String username = mUsername.getText().toString();
        String pwd = mPwd.getText().toString();
        String clientId = JPushInterface.getRegistrationID(m.mContext);
        Call<LoginedUser> call = mAuthService.login(username, pwd, clientId);
        call.enqueue(new RetrofitCallback<LoginedUser>() {
            @Override
            public void onResponse(Call<LoginedUser> call, Response<LoginedUser> response) {
                m.showLoading(false);
                if (!response.isSuccessful())
                    return;
                LoginedUser user = response.body();
                if (user.code != 1) {
                    Toast.makeText(m.mContext, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    m.setLoginedUser(user);
                    Toast.makeText(m.mContext, "登录成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(m.mContext, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void onFailure(Call<LoginedUser> call, Throwable t) {
                m.showLoading(false);
                Toast.makeText(m.mContext, "网络异常", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onValidationSucceeded() {
        mIsPass = true;
    }

    @Override
    public void onValidationFailed(List<ValidationError> errors) {
        String msg = errors.get(0).getCollatedErrorMessage(m.mContext);
        Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        mIsPass = false;
    }
}
